package com.example.utils.util.GetPost;


import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * TODO   Get Post 工具类
 *
 * @ClassName GetPostUtil
 * @Author xiaojiang
 * @Date 2021/9/27 下午 17:38
 */
public class GetPostUtil {

    /**
     * post请求测试
     */
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    /**
     * GET方法测试
     */
    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();

            HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
            httpURLConnection.setRequestMethod("GET");// 提交模式
            // 设置通用的请求属性
            httpURLConnection.setRequestProperty("accept", "*/*");
            httpURLConnection.setRequestProperty("connection", "Keep-Alive");
            httpURLConnection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            httpURLConnection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = httpURLConnection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
//                System.out.println(key + "--->" + map.get(key));
//                result +=key;
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 向指定 URL 发送Put或者Delete方法的请求
     */
    public static String sendDelete(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.setRequestMethod("DELETE");
//            connection.setRequestProperty("timestamp", timestamp);
//            connection.setRequestProperty("CompanyCode", CompanyCode);
//            connection.setRequestProperty("companyPassword", companyPassword);
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
                result += key;
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送DELETE请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    /**
     * post请求测试
     */
    public static String sendPostHeader(String url, String param, String bearer) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Authorization", "Bearer " + bearer);
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！" + e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }


    public static void main(String[] args) {
        JSONObject json_test1 = null;
        String path = "http://" + "192.168.3.160" + ":80/oauth/token";
        String sccessToken="eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImUwNmY3YmVlMTRhMzEwZWIzMTQyOGU0NWM3OGU1OTc1YWE1NDU5MDdkNTg5Y2Y4YmM1ZGI5NjQ5N2Y2NGJjZmRiMzI1YWU4YTU3YTQxMmU0In0.eyJhdWQiOiIyIiwianRpIjoiZTA2ZjdiZWUxNGEzMTBlYjMxNDI4ZTQ1Yzc4ZTU5NzVhYTU0NTkwN2Q1ODljZjhiYzVkYjk2NDk3ZjY0YmNmZGIzMjVhZThhNTdhNDEyZTQiLCJpYXQiOjE1NTU0OTE4NDUsIm5iZiI6MTU1NTQ5MTg0NSwiZXhwIjoxNTg3MTE0MjQ1LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.a9TKGN-39NIlqElhhyu3Tk0nAUls8Cx9r5D74U3-jWPxBQac3bKgqubomFcWKsyzUSHgcmf6A-22YGvlCy3GmqT3wL5CrNRqPSsBOItFuT-1nD1WbvEfO9L8uMX_TmAXzAaCnyshEhrzyzzuboGGSnccJP-SenTEJReWekmHGFzT5qCwFoz2TT7veggcJqVARzM7GBneHi8r0LUXMKbG1Uv2hREb-ShQkeSNzI4zmUAo8kOZMBSLXOREpAid7Kk8vOVix7UQ6v7hnNjItnuLx039ZS-0ZUqOzsZ_gSOp1HEdbEVx8IHl1JxZf6db_NtQEX9PYjBRuQWfwNds6HaBsz31FfSYSwXjGx39XZf8hFQfcQDs-u1Et_Aao4zIwmzcXcopGrKuHiirXp-fns8mz0nrGAQeCrZKSdqTy65aYRju6jNXp-NN5Q9jlPKrfASEDXvakifqlK9WLqzHgJcZP31oow1eaRXAuxZiKgzYMdNFIqDp0PlH_qzG9QQbuz9gmkF3Xl7RB2HQwAfQUDvMN_1-1yXq8bQi741beLS4Ie2slTdcIsbJGgAtEiKRlah11zlYYwH6ecPkBJw298tWXiZZSEK19n61yiO17KEQgS-UHGVZL7L1deLAhJLYl5IznucUtODc6zrvvE67Ta5tZgaHg16UpCxjvxV4645zI5c";
//        String sss = GetPostUtil.sendPostHeader(path, "", sccessToken);
        String po = "grant_type=password&client_id=2&client_secret=bEndz5kxKF1Qhx9wP1FXucSpFTb4uCb8T0rcyyHf&username=" + "dotouch@dotouch.com.cn" + "&password=" + "dotouch" + "&scope=" + "";
        String sss = GetPostUtil.sendPost(path, po);
//        String sss = GetPostUtil.sendPostHeader(path, "", sccessToken);
//        String sss = GetPostUtil.sendPostHeader(path, "project_id=66", sccessToken);

        System.out.println(path + ">>>>>>>>>>>>>>>");
        System.out.println("-------------------------");
        System.out.println("---返回信息：" + sss);
        json_test1 = JSONObject.parseObject(sss);
        System.out.println(json_test1);
        JSONObject data = json_test1.getJSONObject("data");
//        String id1 = data.getString("project_id");
////        System.out.println("-----"+id1);


    }



}
